#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int smallestRangeII(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        //if(n == 1) return 0;
        int min1 = nums[0], max1 = nums[n - 1];
        int res = max1 - min1;
        for (int i = 0; i < n - 1; i++)
        {
            int front = nums[i], back = nums[i + 1];
            res = min(res, max(max1 - k, front + k) - min(min1 + k, back - k));
        }
        return res;
    }
};